//
//  MovieDataBaseHandle.m
//  UI豆瓣2015到2016跨年作业
//
//  Created by LHY on 16/1/8.
//  Copyright © 2016年 Lanou 32 李昊宇. All rights reserved.
//

#import "MovieDataBaseHandle.h"
#import "ActivityModel.h"
@implementation MovieDataBaseHandle


- (NSMutableArray *)selectModel1:(ActivityModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"select * from activity"];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    NSMutableArray *arrModel = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            const unsigned char *begin_time = sqlite3_column_text(stmt, 2);
            const unsigned char *end_time = sqlite3_column_text(stmt, 3);
            const unsigned char *address = sqlite3_column_text(stmt, 4);
            const unsigned char *image = sqlite3_column_text(stmt, 5);
            const unsigned char *category_name = sqlite3_column_text(stmt, 6);
            int wisher_count = sqlite3_column_int(stmt, 7);
            int participant_count = sqlite3_column_int(stmt, 8);
            ActivityModel *model = [ActivityModel activityModel];
            model.title = [NSString stringWithUTF8String:(const char *)title];
            model.begin_time = [NSString stringWithUTF8String:(const char *)begin_time];
             model.end_time = [NSString stringWithUTF8String:(const char *)end_time];
            //title text,begin_time text,end_time text,address text,image text,category_name,wisher_count integer,participant_count integer
            model.address = [NSString stringWithUTF8String:(const char *)address];
            model.image = [NSString stringWithUTF8String:(const char *)image]; model.category_name = [NSString stringWithUTF8String:(const char *)category_name];
            model.wisher_count = [NSNumber numberWithInt:wisher_count];
            model.participant_count = [ NSNumber numberWithInt:  participant_count];
            [arrModel addObject:model];
        }
    }
    

    return arrModel;


}


- (void)deleteModel:(ActivityModel *)model {

    NSString *sqlStr = [NSString stringWithFormat:@"delete from activity where title = '%@'", model.title];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
    
        NSLog(@"删除失败");
    
    }


}



- (BOOL)selectModel:(ActivityModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"select * from activity where title = '%@'", model.title];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
//    NSMutableArray *arrModel = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            return 1;
        }
    }
        return 0;
}



//lll

+ (MovieDataBaseHandle *)movieDataBaseHandle {
    static MovieDataBaseHandle *handle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[MovieDataBaseHandle alloc] init];
    });
    return handle;
}

- (void)openDataBase {

    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbPath = [document stringByAppendingPathComponent:@"Moive.sqlite"];
    NSLog(@"%@", dbPath);
    int result = sqlite3_open(dbPath.UTF8String, &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    }else {
        NSLog(@"打开失败");
    }

}

- (void)createTable {

    NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists activity (number integer primary key autoincrement,title text,begin_time text,end_time text,address text,image text,category_name,wisher_count integer,participant_count integer)"];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"建表成功");
    } else {
    
        NSLog(@"建表失败");
    }
}



- (void)insertModel:(ActivityModel *)model {
    NSInteger wisher_count = [model.wisher_count intValue];
    NSInteger participant_count = [model.participant_count intValue];
    
    NSString *sqlStr = [NSString stringWithFormat:@"insert into activity (title, begin_time, end_time, address, image , category_name, wisher_count, participant_count) values('%@','%@','%@','%@','%@','%@','%ld','%ld')",model.title , model.begin_time, model.end_time, model.address, model.image, model.category_name, wisher_count , participant_count];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"收藏成功");
    } else {
    
        NSLog(@"收藏失败");
    }
}







@end
